<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <input type="text" v-model="keyword" placeholder="关键字">
        <button @click="sortType = 'asc'">升序</button>
        <button @click="sortType = 'deac'">降序</button>
        <button @click="sortType = 'default'">原顺序</button>
        <ul>
            <li v-for="p in filterPresons" :key="p.id">
                {{p.name}} - {{p.age}}
            </li>
        </ul>
    </div>
    
</body>
<script type="text/javascript">
    Vue.config.productionTip = false //以阻止 vue 在启动时生成生产提示。
    //创建Vue
    const vm = new Vue({
        el:'#root', 
        data:{
            keyword:'',
            sortType:'default',
            presons:[
                {id:1,name:'马冬梅',age:50},
                {id:2,name:'周冬雨',age:19},
                {id:3,name:'周杰伦',age:30},
                {id:4,name:'温兆伦',age:21},
            ],
        },
        computed:{
            filterPresons(){
                const arr =  this.presons.filter((item)=>{
                    return item.name.indexOf(this.keyword) !== -1
                })
                if(this.sortType !=='default'){
                    arr.sort((a,b)=>{
                        return this.sortType ==='asc' ? a.age - b.age : b.age - a.age
                    })
                }
                return arr
            }
        }
       
    })

</script>
</html>